package com.learn.controller;

import com.github.pagehelper.PageInfo;
import com.learn.dto.User;
import com.learn.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

// 添加日志
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     * 
     * @return list<User></>
     */
    @GetMapping("/list")
    public List<User> list() {
        log.info("查询全部用户");
        return userService.findAll();
    }

    /**
     * 根据id查询用户
     * 
     * @param id
     * @return user
     */
    @GetMapping("/findById")
    public User findById(Integer id) {
        log.info("根据id查询用户", id);
        return userService.findById(id);
    }

    @PostMapping("/insert")
    public int insert(@RequestBody User user) {
        log.info("添加用户", user);
        return userService.insert(user);
    }

    // 分页查询
    @GetMapping("/page")
    public List<User> page(@RequestParam(value = "pageNow", defaultValue = "0") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        log.info("分页查询用户");

        return userService.getPage(pageNum, pageSize);
    }

    @GetMapping("/pageCount")
    public PageInfo<User> pageCount(
            @RequestParam(value = "pageNow", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            String name, Short gender, String phone,
            String account, Short status, String deptId,
            LocalDateTime beginTime, LocalDateTime endTime

    ) {
        log.info("分页查询用户222");
        return userService.getPageCount(pageNum, pageSize, name, gender, phone, account, status, deptId, beginTime,
                endTime);
    }

    // 删除
    @DeleteMapping("/delete")
    public int delete(int id) {
        log.info("删除用户", id);
        return userService.delete(id);
    }

    @DeleteMapping("/deletes")
    public int deletes(List<Integer> ids) {
        log.info("批量删除用户", ids);
        return userService.deletes(ids);
    }
}
